/**
 * 
 */
package com.lanswon.qzsmk.service.systemManageService.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ch.qos.logback.classic.Logger;

import com.alibaba.fastjson.JSONObject;
import com.lanswon.qzsmk.base.AccountContants;
import com.lanswon.qzsmk.base.AppContants;
import com.lanswon.qzsmk.base.CardContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.dao.AcMerchantMapper;
import com.lanswon.qzsmk.dao.AddressAreaMapper;
import com.lanswon.qzsmk.dao.AddressCityMapper;
import com.lanswon.qzsmk.dao.AddressProvinceMapper;
import com.lanswon.qzsmk.dao.AddressStreetMapper;
import com.lanswon.qzsmk.dao.AppClassificationMapper;
import com.lanswon.qzsmk.dao.AppKindMapper;
import com.lanswon.qzsmk.dao.AppOpenRegMapper;
import com.lanswon.qzsmk.dao.ApplyBatchRecordMapper;
import com.lanswon.qzsmk.dao.CardDataMapper;
import com.lanswon.qzsmk.dao.CardLossMapper;
import com.lanswon.qzsmk.dao.CardPhysicsTypeMapper;
import com.lanswon.qzsmk.dao.CardProdunctDefinitionMapper;
import com.lanswon.qzsmk.dao.CardTypeMapper;
import com.lanswon.qzsmk.dao.CertificateMapper;
import com.lanswon.qzsmk.dao.NationMapper;
import com.lanswon.qzsmk.dao.PaperTypeMapper;
import com.lanswon.qzsmk.dao.PersonalMapper;
import com.lanswon.qzsmk.dao.SamCardMapper;
import com.lanswon.qzsmk.dao.TxnHisDtlMapper;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.exception.ErrorCode;
import com.lanswon.qzsmk.exception.SystemException;
import com.lanswon.qzsmk.model.AcMerchant;
import com.lanswon.qzsmk.model.AddressArea;
import com.lanswon.qzsmk.model.AddressCity;
import com.lanswon.qzsmk.model.AddressProvince;
import com.lanswon.qzsmk.model.AddressStreet;
import com.lanswon.qzsmk.model.AppClassification;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.model.AppOpenReg;
import com.lanswon.qzsmk.model.ApplyBatchRecord;
import com.lanswon.qzsmk.model.CardLoss;
import com.lanswon.qzsmk.model.CardPhysicsType;
import com.lanswon.qzsmk.model.CardProdunctDefinition;
import com.lanswon.qzsmk.model.Certificate;
import com.lanswon.qzsmk.model.PaperType;
import com.lanswon.qzsmk.model.Personal;
import com.lanswon.qzsmk.model.SamCard;
import com.lanswon.qzsmk.model.TxnHisDtl;
import com.lanswon.qzsmk.service.preService.BikeService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.util.CitizenCardNoGeneratorUtil;
import com.lanswon.qzsmk.util.CommonUtil;
import com.lanswon.qzsmk.util.DynamicDataSourceHolder;
import com.lanswon.qzsmk.util.ExportExcel;
import com.lanswon.qzsmk.util.FileUtil;
import com.lanswon.qzsmk.vo.BikeConsumeRecord;
import com.lanswon.qzsmk.vo.CardProductListVo;
import com.lanswon.qzsmk.vo.CardProdunctVo;
import com.lanswon.qzsmk.vo.PersonalCardInfoAndConsumeRecords;
import com.lanswon.qzsmk.vo.ResultObject;
import com.lanswon.qzsmk.vo.ResultStatus;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.SamCardVo;
import com.lanswon.qzsmk.vo.TransactBillVo;

/**
 * @author sun
 *
 */
@Service
public class ParamManageServiceImpl implements ParamManageService {
	
	private static Logger logger = (Logger) LoggerFactory.getLogger(ParamManageServiceImpl.class);


	@Autowired
	CardPhysicsTypeMapper cptm;

	@Autowired
	CardProdunctDefinitionMapper cpdm;

	@Autowired
	PaperTypeMapper ptm;

	@Autowired
	CardTypeMapper ctm;

	@Autowired
	CertificateMapper cm;

	@Autowired
	AddressAreaMapper aam;

	@Autowired
	AddressStreetMapper asm;

	@Autowired
	AddressProvinceMapper apm;

	@Autowired
	AddressCityMapper acm;

	@Autowired
	NationMapper nm;

	@Autowired
	CardLossMapper clm;

	@Autowired
	AppKindMapper akm;
	
	@Autowired
	AppClassificationMapper acfm;

	@Autowired
	PersonalMapper pm;

	@Autowired
	CardDataMapper cdm;

	@Autowired
	TxnHisDtlMapper thdm;

	@Autowired
	AcMerchantMapper amm;
	
	@Autowired
	SamCardMapper scm;
	
	@Autowired
	ApplyBatchRecordMapper abrm;
	
	@Autowired
	BikeService bs;
	
	@Autowired
	AppOpenRegMapper aorm;

	@Override
	public CardProdunctDefinition queryCardPCode(String cardProductCode) throws Exception {
		if (StringUtils.isEmpty(cardProductCode)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "object param is null");
		}

		return cpdm.queryCardPCode(cardProductCode);
	}

	@Override
	public PaperType queryPaperByPaperTypeCode(String paperTypeCode) throws Exception {
		if (StringUtils.isEmpty(paperTypeCode)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "object param is null");
		}

		return ptm.queryPaperTypeByCode(paperTypeCode);
	}

	@Override
	public PaperType queryPaperByPaperName(String paperName) throws ApplicationException {
		if (StringUtils.isEmpty(paperName)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "object param is null");
		}

		return ptm.queryPaperTypeByPaperName(paperName);
	}

	@Override
	public String queryCardTypeNameByCode(String code) throws Exception {
		if (StringUtils.isEmpty(code)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "object param is null");
		}

		return ctm.queryCardTypeByCode(code).getCardTypeName();
	}

	@Override
	public CardPhysicsType queryCardPhysicsTypeByCode(String code) throws Exception {
		if (StringUtils.isEmpty(code)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "object param is null");
		}

		return cptm.queryCardPhysicsTypeByCode(code);
	}

	@Override
	public ResultVo queryAllCardPhysicsType() {

		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);

		ResultObject ro = new ResultObject();
		ro.setItems(cptm.queryAllCardPhysicsType());
		rv.setServiceObject(ro);
		return rv;

	}

	@Override
	public ResultVo queryAllCardProdunctDefinition() {

		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		CardProdunctVo cpv=new CardProdunctVo();
		cpv.setValidStatus(CardContants.STATUS_ENABLE);
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);

		ResultObject ro = new ResultObject();
		ro.setItems(cpdm.queryAllCardProdunct(cpv));
		rv.setServiceObject(ro);
		return rv;
	}
	
	@Override
	public ResultVo queryAllCertificates() {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);

		ResultObject ro = new ResultObject();
		List<Certificate> list = cm.queryAllCertificates();
		if(list.size()==0){
			rs.setCode(HttpContants.RETURN_ERROR);
			rs.setMessage("查询证件类型失败，请联系系统管理员");
			rv.setResultStatus(rs);
			return rv;
		}
		ro.setItems(list);
		rv.setServiceObject(ro);
		return rv;
	}

	@Override
	public CardProdunctDefinition queryCardTypeCodeByName(String name) throws Exception {
		if (StringUtils.isEmpty(name)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "cardPName is null!!!");
		}
		return cpdm.queryCardPCodeByCardProductName(name);
	}

	@Override
	public List<AddressArea> queryAddressAreaByFatherId(String id) throws ApplicationException {
		if (StringUtils.isEmpty(id)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "father id is null!!!");
		}
		return aam.queryAddressAreaByFatherId(Long.valueOf(id));
	}

	@Override
	public List<AddressCity> queryAddressCityByFatherId(String id) throws ApplicationException {
		if (StringUtils.isEmpty(id)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "father id is null!!!");
		}
		return acm.queryAddressCityByFatherId(Long.valueOf(id));
	}

	@Override
	public List<AddressProvince> queryAddressProvince() {

		return apm.queryAddressProvince();
		
	}

	@Override
	public List<AddressStreet> queryAddressStreetByFatherId(String id) throws ApplicationException {
		if (StringUtils.isEmpty(id)) {
			throw new ApplicationException(ErrorCode.OBJECT_IS_NULL, "father id is null!!!");
		}
		return asm.queryAddressStreetByFatherId(Long.valueOf(id));
	}

	@Override
	public ResultVo queryNation() {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		ResultObject ro = new ResultObject();
		ro.setItems(nm.queryNation());
		rv.setServiceObject(ro);
		return rv;
	}

	@Override
	public List<CardLoss> queryCardLoss() {
		return clm.queryCardLoss();
	}

	@Override
	public List<AppKind> queryAppKind() {
		return akm.queryAppKind();
	}
	
	@Override
	public List<AppKind> queryAppKindByKindID(String appKindId) {
		
		return akm.queryAppKindByKindID(Integer.valueOf(appKindId));
	}
	
	@Override
	public List<AppClassification> queryAppClassification() {
		
		return acfm.queryAppClassification();
	}
	
	@Override
	public ResultVo queryCardProdunctByPersonal(String paperType, String paperNo, String cardPCode){
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		Certificate cf = cm.queryCertificateByCode(paperType);
		String paperTypeName = cf.getCertificatesName();
		
		if (StringUtils.isEmpty(paperType) || StringUtils.isEmpty(paperNo)) {
			rs.setCode(ErrorCode.OBJECT_IS_NULL);
			rs.setMessage("证件类型或证件号码不能为空");
			rv.setResultStatus(rs);
			return rv;
		}
		int count = pm.queryPersonalCountByPaperNo(paperType, paperNo);
		if(count == 0){
			rs.setCode(ErrorCode.USER_NOT_EXIST);
			rs.setMessage("证件类型[" + paperTypeName + "],证件号码[" + paperNo + "]查询无此人信息");
			rv.setResultStatus(rs);
			return rv;
		}
		
		List<CardProductListVo> lc = cdm.queryCardDataByPaperNoAndCardStatus_1(paperType, paperNo, cardPCode,
				CardContants.CARD_STATE_NORMAL,CardContants.CARD_STATE_OFFICIAL_LOSS,CardContants.CARD_STATE_ORAL_LOSS);
		
		
		
		if (lc.size() == 0) {
			rs.setCode(ErrorCode.OBJECT_IS_NULL);
			rs.setMessage("证件类型[" + paperTypeName + "],证件号码[" + paperNo + "]查询卡片未激活或者已注销");
			rv.setResultStatus(rs);
			return rv;
		}
		
		
		List<CardProductListVo> lv = new ArrayList<>();
		
		for(CardProductListVo cplv:lc){
			
			cplv.setCardpName(cpdm.queryCardPCode(cplv.getCardpCode()).getCardpName());
			cplv.setCardTypeName(ctm.queryCardTypeByCode(cplv.getCardTypeCode()).getCardTypeName());
			cplv.setPaperTypeName(paperTypeName);
			cplv.setCardStatusName(CardContants.getCardStatusNameByCode(cplv.getCardStatus()));
			logger.debug("卡状态："+cplv.getCardStatus());
			lv.add(cplv);
		}
		
		
		
		ResultObject ro = new ResultObject();
		
		ro.setItems(lv);
		
		rv.setServiceObject(ro);
		
		return rv;
		
	}
	
	


	@Override
	public ResultVo queryPersonalCardInfoAndConsumeRecordsByPaperNo(String citizenCardNo, String year,String month,String tradeCondition,String appFlag) {
		if("1".equals(appFlag)){
			appFlag = AppContants.APP_FLAG_TRANSIT;
		}else if("2".equals(appFlag)){
			appFlag = AppContants.APP_FLAG_BIKE;
		}
		
		Calendar now = Calendar.getInstance();
		if(StringUtils.isEmpty(year)){
			year = String.valueOf(now.get(Calendar.YEAR));
		}
		
		if(StringUtils.isEmpty(month)){
			month = String.valueOf(now.get(Calendar.MONTH) + 1);
		}
		
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		
		
		List<PersonalCardInfoAndConsumeRecords> list = new ArrayList<>();

		PersonalCardInfoAndConsumeRecords pc = new PersonalCardInfoAndConsumeRecords();
		
		String newBalance ="";
		String appName =akm.queryAppKindByAppFalg(appFlag).getAppName();
		pc.setAppName(appName);
	if(AppContants.APP_FLAG_BIKE.equals(appFlag)){
		AppOpenReg aor = aorm.queryAppOpenRegByCardNoAndFlag(citizenCardNo, appFlag);
		if(aor == null ){
			 rs = new ResultStatus();
			rs.setCode(HttpContants.RETURN_ERROR);
			rs.setMessage("自行车应用未开通");
			rv.setResultStatus(rs);
			return rv;
		}else if(AppContants.APP_STATUS_CLOSE.equals(aor.getAppStatus()) ){
			 rs = new ResultStatus();
			rs.setCode(HttpContants.RETURN_ERROR);
			rs.setMessage("自行车应用已关闭");
			rv.setResultStatus(rs);
			return rv;
		}
		
		JSONObject js = null;
		try {
			js = bs.queryBikeUserByCitizenCardNo(citizenCardNo);
		} catch (ApplicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			 rs = new ResultStatus();
				rs.setCode(e.getErrorCode());
				rs.setMessage(e.getMessage());
				rv.setResultStatus(rs);
				return rv;
		}
		js = js.getJSONObject("body");
		newBalance = js.getString("balance");
		pc.setNewBalance(CommonUtil.convertMoney_1(newBalance).setScale(2,BigDecimal.ROUND_HALF_UP));
		
	}else if(AppContants.APP_FLAG_TRANSIT.equals(appFlag)){
		TxnHisDtl t = thdm.queryLastTxnHisDtl(citizenCardNo,appFlag);
		pc.setNewBalance(t == null?new BigDecimal(0).setScale(2,BigDecimal.ROUND_HALF_UP):t.getTxnBalAft().setScale(2,BigDecimal.ROUND_HALF_UP));
	}
		
	AcMerchant bike = new AcMerchant();
			List<TransactBillVo> lll = new ArrayList<>();
			List<TxnHisDtl> lt = thdm.queryTxnHisDtlByCitizenCardNo(citizenCardNo, year, month,appFlag);
			if (lt.size() > 0) {
				for (TxnHisDtl thd : lt) {
					if(AccountContants.ACCEPTANCE_TYPE_NET.equals(thd.getAcceptTp()) && AccountContants.TXNTP_CONSUMPTION.equals(thd.getTxnAttr())) {
						continue;
					}
					
					TransactBillVo tbv = new TransactBillVo();
					
					tbv.setAcTypeName(AccountContants.getNameByTxnType(thd.getTxnAttr()));
					
					
					
					tbv.setTxnBalAft(thd.getTxnBalAft().setScale(2,BigDecimal.ROUND_HALF_UP));
					tbv.setTxnAmt(thd.getTxnAmt().setScale(2,BigDecimal.ROUND_HALF_UP));
					
					try {
						Date d = DateUtils.parseDate(thd.getTxnDt(), "yyyyMMddHHmmss");
						String time = DateFormatUtils.format(d,"yyyy-MM-dd HH:mm:ss");
						tbv.setCreDate(time);
					} catch (ParseException e) {
						e.printStackTrace();
						String msg="日格格式化异常";
						logger.error(msg);
						throw new SystemException(SystemException.ParseException, msg);

					}
				
					
					
					AcMerchant am = amm.queryAcMerchantByMctCode(thd.getAcceptCusNo());
					if(AppContants.APP_FLAG_BIKE.equals(appFlag)){
						bike = am;
					}
					tbv.setOperatCompany(am.getMctName());
					
					
					tbv.setOrderNo(thd.getBusinessNo());
					
					if(StringUtils.isEmpty(tradeCondition) || "0".equals(tradeCondition)){
						lll.add(tbv);

						
					}else if("1".equals(tradeCondition)){
						if(AccountContants.TXNTP_RECHARGE.equals(thd.getTxnAttr())){
							lll.add(tbv);
						}
					}else if("2".equals(tradeCondition)){
						if(AccountContants.TXNTP_CONSUMPTION.equals(thd.getTxnAttr())){
							lll.add(tbv);
							
							
							
						}
					}
					
				}
				
				 if( AppContants.APP_FLAG_BIKE.equals(appFlag)){
					 if("2".equals(tradeCondition) || "0".equals(tradeCondition)){
						//查询自行车消费记录
							Calendar c = Calendar.getInstance();    
							c.add(Integer.valueOf(month), 0);
						    c.set(Calendar.DAY_OF_MONTH,1);
						    
						    
						    Calendar ca = Calendar.getInstance();  
						    c.add(Integer.valueOf(month), 0);
						    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
						       
							try {
								List<BikeConsumeRecord> list_bike = bs.bikeAppqueryConsumeRecord(citizenCardNo, DateFormatUtils.format(c, "YYYYMMDD"), DateFormatUtils.format(ca, "YYYYMMDD"));
							
							for(BikeConsumeRecord bcr:list_bike){
								TransactBillVo tbv2 = new TransactBillVo();
								tbv2.setTxnBalAft(new BigDecimal(-1));
								tbv2.setTxnAmt(new BigDecimal(bcr.getCost()).setScale(2,BigDecimal.ROUND_HALF_UP));
								tbv2.setAcTypeName("消费");
								tbv2.setCreDate(bcr.getReturntime());
								tbv2.setOperatCompany(bike.getMctName());
								tbv2.setOrderNo(bcr.getBikeno()+bcr.getBorrowtime());
								tbv2.setBikeno(bcr.getBikeno());
								tbv2.setBorrowtime(bcr.getBorrowtime());
								tbv2.setBorrowstation(bcr.getBorrowstation());
								tbv2.setReturntime(bcr.getReturntime());
								tbv2.setReturnstation(bcr.getReturnstation());
								tbv2.setTradedate(bcr.getTradedate());
								
								lll.add(tbv2);
							}
								
							} catch (ApplicationException e) {
								e.printStackTrace();
								 rs = new ResultStatus();
								rs.setCode(e.getErrorCode());
								rs.setMessage(e.getMessage());
								rv.setResultStatus(rs);
								
								return rv;
							}
					 }
						
				 }
				
				
				
				
				pc.setTransactionRecords(lll);
			}else{
				//查询历史表 无记录
				if("2".equals(tradeCondition) && AppContants.APP_FLAG_BIKE.equals(appFlag)){
					//查询自行车消费记录
					Calendar c = Calendar.getInstance();    
					c.add(Integer.valueOf(month), 0);
				    c.set(Calendar.DAY_OF_MONTH,1);
				    
				    
				    Calendar ca = Calendar.getInstance();  
				    c.add(Integer.valueOf(month), 0);
				    ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
				       
					try {
						List<BikeConsumeRecord> list_bike = bs.bikeAppqueryConsumeRecord(citizenCardNo, DateFormatUtils.format(c, "YYYYMMDD"), DateFormatUtils.format(ca, "YYYYMMDD"));
					
					for(BikeConsumeRecord bcr:list_bike){
						TransactBillVo tbv2 = new TransactBillVo();
						tbv2.setTxnBalAft(new BigDecimal(-1));
						tbv2.setTxnAmt(new BigDecimal(bcr.getCost()).setScale(2,BigDecimal.ROUND_HALF_UP));
						tbv2.setAcTypeName("消费");
						tbv2.setCreDate(bcr.getReturntime());
						tbv2.setOperatCompany("青州自行车公司");
						tbv2.setOrderNo(bcr.getBikeno()+bcr.getBorrowtime());
						tbv2.setBikeno(bcr.getBikeno());
						tbv2.setBorrowtime(bcr.getBorrowtime());
						tbv2.setBorrowstation(bcr.getBorrowstation());
						tbv2.setReturntime(bcr.getReturntime());
						tbv2.setReturnstation(bcr.getReturnstation());
						tbv2.setTradedate(bcr.getTradedate());
						
						lll.add(tbv2);
					}
						
					} catch (ApplicationException e) {
						e.printStackTrace();
						 rs = new ResultStatus();
						rs.setCode(e.getErrorCode());
						rs.setMessage(e.getMessage());
						rv.setResultStatus(rs);
						
						return rv;
					}
					
				}else if(AppContants.APP_FLAG_TRANSIT.equals(appFlag)){
					rs = new ResultStatus();
					rs.setCode(HttpContants.RETURN_ERROR);
					rs.setMessage("暂无交易记录信息");
					rv.setResultStatus(rs);
					return rv;
				}
				
				
				 
			}
			
			list.add(pc);

		rv.setResultStatus(rs);

		ResultObject ro = new ResultObject();
		ro.setItems(list);
		rv.setServiceObject(ro);

		return rv;

	}

	@Override
	public ResultVo querySamCardbyPage(SamCardVo vo) {
		DynamicDataSourceHolder.setDataSource("dataSource_sam");
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		List<SamCard> list = scm.querySamCardbyPage(vo);
		int totalCount = scm.querySamCardCountbyPage(vo);
		
		DwzPage<SamCard> dp = new DwzPage<>(vo.getCurrentPage(), vo.getNumPerPage(), totalCount);
		dp.setList(list);
		
		rv.setServiceObject(dp);
		DynamicDataSourceHolder.setDataSource("dataSource");
		return rv;
		
	}

	@Override
	public ResultVo generateApplyBatchNo(String cardPCode, String cardPhysicalType) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);

		ApplyBatchRecord abr = new ApplyBatchRecord();
		abr.setCardpCode(cardPCode);
		abr.setCardPhysicalType(cardPhysicalType);
		abr.setDate(new Date());
		abr.setDesc("空白卡申请任务");
		
		ApplyBatchRecord a = abrm.queryNewestRecord();
		String seq ="";
		if(a == null){
			seq =CitizenCardNoGeneratorUtil.generatorSeq("1", 6);
		}else{
			String  previousSeq = a.getSeq();
			Integer s = Integer.valueOf(previousSeq)+1;
			seq = CitizenCardNoGeneratorUtil.generatorSeq(String.valueOf(s), 6);
			
		}
		abr.setSeq(seq);
		abr.setBatchNo(cardPCode+"_"+cardPhysicalType+"_"+seq);
		
		
		ResultObject ro = new ResultObject();
		ro.setStr(abr.getBatchNo());
		rv.setServiceObject(ro);
		
		return rv;
	}

	
	@Override
	public ResultVo exportApplyTemplate(String root,String cardPCode, String cardPhysicalType) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		ResultVo v = generateApplyBatchNo(cardPCode, cardPhysicalType);
		if(v.getResultStatus().getCode() == HttpContants.RETURN_SUCCESS){
			ResultObject o = (ResultObject) v.getServiceObject();
		String path = ExportExcel.exportTemplate(root,FileUtil.generatorPath1(root, o.getStr()));
		ResultObject ro = new ResultObject();
		ro.setStr(path);
		rv.setServiceObject(ro);
		
		}else{
			rs.setCode(ErrorCode.COMMON_EXCEPTION);
			rs.setMessage("生成模板失败");
			rv.setResultStatus(rs);
		}
		return rv;
	}

	
	@Override
	public ResultVo queryNationByCode(String numbercode) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		
		
		if (StringUtils.isEmpty(numbercode)) {
			numbercode ="1";
		}
		
		rv.setServiceObject(nm.queryNationByCode(numbercode));
		return rv;
	}	
	
	@Override
	public ResultVo queryNationByName(String name) {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		if (StringUtils.isEmpty(name)) {
			name ="汉族";
		}
		rv.setServiceObject(nm.queryNationByName(name));

		return rv;
	}

	@Override
	public ResultVo queryAllCardProdunctDefinition_internet() {
		ResultVo rv = new ResultVo();
		ResultStatus rs = new ResultStatus();
		rs.setCode(HttpContants.RETURN_SUCCESS);
		rs.setMessage("返回成功");
		rv.setResultStatus(rs);
		
		ResultObject ro = new ResultObject();
		ro.setItems(cpdm.queryAvailableCardProdunctDefinitionByInternet(CardContants.CARD_TYPE_REGISTERED, CardContants.CARD_PROPERTY_COMMON,CardContants.STATUS_ENABLE));
		rv.setServiceObject(ro);

		return rv;
	}

	
	
}
